dnl config.m4 for extension jq

dnl Comments in this file start with the string 'dnl'.
dnl Remove where necessary.

dnl If your extension references something external, use 'with':

PHP_ARG_WITH([jq],
  [for jq support],
  [AS_HELP_STRING([--with-jq],
    [Include jq support])])

dnl Otherwise use 'enable':

PHP_ARG_ENABLE([jq],
  [whether to enable jq support],
  [AS_HELP_STRING([--enable-jq],
    [Enable jq support])],
  [no])

if test "$PHP_JQ" != "no"; then
  dnl 检查 jq 库
  AC_PATH_PROG(PKGCONFIG, pkg-config, no)

  if test -x "$PKGCONFIG"; then
    AC_MSG_CHECKING([for libjq])
    if $PKGCONFIG --exists libjq; then
      JQ_CFLAGS=`$PKGCONFIG --cflags libjq`
      JQ_LIBS=`$PKGCONFIG --libs libjq`
      JQ_VERSION=`$PKGCONFIG --modversion libjq`
      AC_MSG_RESULT([found version $JQ_VERSION])
      PHP_EVAL_INCLINE($JQ_CFLAGS)
      PHP_EVAL_LIBLINE($JQ_LIBS, JQ_SHARED_LIBADD)
    else
      AC_MSG_RESULT([not found])
      AC_MSG_ERROR([libjq not found])
    fi
  else
    dnl 如果没有 pkg-config，尝试手动检测
    PHP_ADD_INCLUDE(/opt/homebrew/include)  # 替换为 Homebrew 中实际的路径
    AC_CHECK_HEADER([jq.h], [], [AC_MSG_ERROR([jq.h header not found])])
    AC_CHECK_LIB([jq], [jq_init], [
      PHP_ADD_LIBRARY(jq, 1, JQ_SHARED_LIBADD)
    ], [
      AC_MSG_ERROR([libjq not found])
    ])
  fi

  PHP_SUBST(JQ_SHARED_LIBADD)
  PHP_NEW_EXTENSION(jq, jq.c, $ext_shared)
fi
